<template>
  <!-- 弹出新增窗口 
        title 窗口的标题
        :visible.sync 当它true的时候，窗口会被弹出
        -->
    <el-dialog 
    :title="pojo.id == null ? '新增商品' : '修改商品'"
    :visible.sync="dialogVisible"
    :before-close="handleClose"
    :close-on-click-modal="false" width="500px">
      <el-form
        :rules="rules"
        ref="pojoForm"
        label-width="100px"
        label-position="right"
        style="width: 400px"
        :model="pojo"
      >
        <el-form-item label="商品名称" prop="name" >
                    <el-input v-model="pojo.name" ></el-input>
                </el-form-item>
                <el-form-item label="商品编码" prop="code" >
                    <el-input v-model="pojo.code" ></el-input>
                </el-form-item>
                <el-form-item label="商品规格" prop="spec" >
                   <el-input v-model="pojo.spec" ></el-input>
                </el-form-item>
                <el-form-item label="零售价" prop="retailPrice">
                    <el-input v-model="pojo.retailPrice" ></el-input>
                </el-form-item>
                <el-form-item label="进货价" prop="purchasePrice">
                    <el-input v-model="pojo.purchasePrice" ></el-input>
                </el-form-item>
                <el-form-item label="库存数量" prop="storageNum">
                    <el-input v-model="pojo.storageNum" ></el-input>
                </el-form-item>

                <el-form-item label="供应商" prop="supplierName">
                    <el-input 
                    readonly
                    @click.native="openAddOrEditSupplier"
                    v-model="pojo.supplierName" placeholder="选择供应商"></el-input>
                </el-form-item>
            </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button
          type="primary"
          @click="submitForm('pojoForm')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
</template>

<script>
import goodsApi from "@/api/goods";
export default {
  data () {
    return {
        rules: {
        // 校验规则
        name: [{ required: true, message: "商品名称不能为空", trigger: "blur" }],
        code: [{ required: true, message: "商品编号不能为空", trigger: "blur" }],
        spec: [{ required: true, message: "商品规格不能为空", trigger: "blur" }],
        supplierName: [{ required: true, message: "供应商不能为空", trigger: "change" }],
      },
    }
  },

  props:["dialogVisible","pojo","selectSupplier","closeDialog","openAddOrEditSupplier"],



  components: {},

  methods: {
      // 提交新增数据
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //提交表单
          this.addOrUpdate();
        }
      });
    },

    async addOrUpdate() {
      await goodsApi.add(this.pojo);
      this.$message("成功");

      //消失
      this.handleClose();
    },


        handleClose(){
          console.log("关闭更新或修改的窗口!!!!!!!")
         this.closeDialog();
        this.$refs['pojoForm'].resetFields()
        //     this.$nextTick(() => { 
        // this.$refs['pojoForm'].resetFields() })
             
        },


  }
}
</script>

<style scoped>
</style>